package demo.bean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import inject.Cat;

//@Service("lifeBean")
// @Component("lifeBean")
// 在 xml 里配置
public class LifeBean implements InitializingBean, DisposableBean, ApplicationContextAware,
BeanFactoryAware, BeanNameAware ,EnvironmentAware {

	private String name;
	private String beanName;
	private BeanFactory beanFactory;
	private Environment environment;
	private ApplicationContext applicationContext;
	
	private List<String> infos = new ArrayList<String>();
	
	// xml 配置与注解混合使用 
	@Value("${zoo.name}")
	private String zoo_name;
	
	@Autowired
	private Cat cat;
	
	@Resource
	private Cat cat1;
	
	
	private String msg;
	

	public LifeBean() {
		String msg = "LifeBean() run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}
	
	

	

	public String getMsg() {
		return msg;
	}





	public void setMsg(String msg) {	
		this.msg = msg;		
		String str = "setMsg(msg) run " + System.currentTimeMillis()+",cat="+cat;
		infos.add(str);
		System.out.println(str);
	}





	@PostConstruct
	public void postConstruct() {
		String msg = "@PostConstruct#postConstruct run " + System.currentTimeMillis()+",cat="+cat;
		infos.add(msg);
		System.out.println(msg);
	}

	@PreDestroy
	public void preDestroy() {
		String msg = "@PreDestroy#preDestroy run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		String msg = "InitializingBean#afterPropertiesSet run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}

	@Override
	public void destroy() throws Exception {
		String msg = "DisposableBean#destroy run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}

	
	public void setName(String name) {
		this.name = name;
		String msg = "setInject#setName run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
		this.applicationContext = applicationContext;
		String msg = "ApplicationContextAware#setApplicationContext run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}


	@Override
	public void setBeanFactory(BeanFactory bf) throws BeansException {		
		this.beanFactory = bf;
		String msg = "BeanFactoryAware#setBeanFactory run " + System.currentTimeMillis();
		infos.add(msg);
		System.out.println(msg);
	}

	@Override
	public void setBeanName(String beanName) {
		this.beanName = beanName;
		String msg = "BeanNameAware#setBeanName run " + System.currentTimeMillis();
		infos.add(msg);

		System.out.println(msg);
	}
	
	@Override
	public void setEnvironment(Environment environment){
		this.environment = environment;
		String msg = "EnvironmentAware#setEnvironment run " + System.currentTimeMillis();
		infos.add(msg);

		System.out.println(msg);

	}

	

	public Environment getEnvironment(){
		return environment;
	}
	
	
	public List<String> getInfo(){
		return infos;
	}
	
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();	
		sb.append("name="+name);
		sb.append(",beanName="+beanName);
		// zoo_name
		sb.append(",zoo_name="+zoo_name);
		sb.append(",msg="+msg);
		sb.append(",cat="+cat);
		sb.append(",cat1="+cat1);
		sb.append(",beanFactory="+beanFactory);
		sb.append(",applicationContext="+applicationContext);
	
		return sb.toString();
	}

}
